const Admin = require('@models/admin');
const { generateAdminToken } = require('@shared/utils/jwt');
const logger = require('@shared/utils/logger');

// 管理员登录
exports.login = async (req, res, next) => {
  try {
    logger.info('收到登录请求:', {
      body: req.body,
      headers: req.headers
    });

    const { username, password } = req.body;

    // 参数验证
    if (!username || !password) {
      logger.warn('登录参数无效:', { username, password });
      return res.status(400).json({
        code: 400,
        message: '用户名和密码不能为空'
      });
    }

    logger.info('查找用户:', username);
    const admin = await Admin.findOne({ where: { username } });
    
    if (!admin) {
      logger.warn('用户不存在:', username);
      return res.status(400).json({
        code: 400,
        message: '用户名或密码错误'
      });
    }

    if (!admin.comparePassword(password)) {
      logger.warn('密码错误:', username);
      return res.status(400).json({
        code: 400,
        message: '用户名或密码错误'
      });
    }

    if (admin.status === 0) {
      logger.warn('账号已禁用:', username);
      return res.status(403).json({
        code: 403,
        message: '账号已被禁用'
      });
    }

    // 更新最后登录时间
    logger.info('更新最后登录时间:', username);
    await admin.update({
      last_login_time: new Date()
    });

    // 生成token
    logger.info('生成token:', username);
    const token = generateAdminToken(admin);

    logger.info('登录成功:', username);
    res.json({
      code: 200,
      message: '登录成功',
      data: {
        token,
        admin: {
          id: admin.id,
          username: admin.username,
          name: admin.name,
          phone: admin.phone
        }
      }
    });
  } catch (err) {
    logger.error('登录过程出错:', err);
    next(err);
  }
};

// 管理员登出
exports.logout = async (req, res, next) => {
  try {
    res.json({
      code: 200,
      message: '登出成功'
    });
  } catch (err) {
    next(err);
  }
};

// 获取管理员信息
exports.info = async (req, res, next) => {
  try {
    const admin = req.admin;
    res.json({
      code: 200,
      message: '获取成功',
      data: {
        id: admin.id,
        username: admin.username,
        name: admin.name,
        phone: admin.phone,
        status: admin.status,
        last_login_time: admin.last_login_time
      }
    });
  } catch (err) {
    next(err);
  }
};

// 更新管理员信息
exports.updateInfo = async (req, res, next) => {
  try {
    const admin = req.admin;
    const { name, phone } = req.body;

    await admin.update({
      name,
      phone
    });

    res.json({
      code: 200,
      message: '更新成功'
    });
  } catch (err) {
    next(err);
  }
};

// 修改密码
exports.updatePassword = async (req, res, next) => {
  try {
    const admin = req.admin;
    const { oldPassword, newPassword } = req.body;

    if (!admin.comparePassword(oldPassword)) {
      return res.status(400).json({
        code: 400,
        message: '原密码错误'
      });
    }

    await admin.update({
      password: newPassword
    });

    res.json({
      code: 200,
      message: '修改成功'
    });
  } catch (err) {
    next(err);
  }
};

// 更新头像
exports.updateAvatar = async (req, res, next) => {
  try {
    const admin = req.admin;
    const { avatar } = req.body;

    if (!avatar) {
      return res.status(400).json({
        code: 400,
        message: '头像不能为空'
      });
    }

    await admin.update({
      avatar
    });

    res.json({
      code: 200,
      message: '更新成功',
      data: {
        avatar
      }
    });
  } catch (err) {
    next(err);
  }
}; 